package com.eatrip.dao.implementation;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eatrip.bean.Commento;
import com.eatrip.dao.CommentiDAO;

@Repository
@Transactional
public class CommentiDAOImple implements CommentiDAO {

	
	private HibernateTemplate hibernateTemplate;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	
	@Override
	public Commento getCommento(int idCommento) {
		Commento commento = new Commento();
		commento =(Commento) hibernateTemplate.find("from Commenti where idCommento = ?", idCommento).get(0);
		if (commento == null)
			return null;
		
		return commento;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Commento> getAllCommenti() {
		return hibernateTemplate.find("from Commenti");
	}

	@Override
	public void insertCommento(Commento commento) {
		hibernateTemplate.saveOrUpdate(commento);
	}

	@Override
	public void updateCommento(Commento commento) {
		hibernateTemplate.update(commento);
	}

	@Override
	public void deleteCommento(Commento commento) {
		hibernateTemplate.delete(commento);
	}

	@Override
	public void deleteAllCommenti() {
		Query q = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("DELETE FROM Commenti");
		int row  = q.executeUpdate();

		if (row==0)
			System.out.println("errore nella cancellazione deleteAllUser");

	}

}
